using DeleteOnTimeTool.Models;

namespace DeleteOnTimeTool.Services
{
    public static class LogService
    {
        private static string GetLogDirectory(AppConfig config)
        {
            var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            Directory.CreateDirectory(dir);
            return dir;
        }

        public static void WriteInfo(AppConfig config, string message)
        {
            Write(config, "INFO", message);
        }

        public static void WriteError(AppConfig config, string message)
        {
            Write(config, "ERROR", message);
        }

        private static void Write(AppConfig config, string level, string message)
        {
            try
            {
                var dir = GetLogDirectory(config);
                var file = Path.Combine(dir, $"{DateTime.Now:yyyyMMdd}.log");
                var line = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{level}] {message}";
                File.AppendAllLines(file, new[] { line });
            }
            catch
            {
                // 忽略日志写入错误
            }
        }
    }
}


